/*
 * Copyright (c) 2011 Seoul National University
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Jung Ho Ahn  ( gajh@snu.ac.kr )
 *
 */

#ifndef __BLOCK_ALLOCATOR__
#define __BLOCK_ALLOCATOR__

#include<stdint.h>
#include<iostream>
#include<pthread.h>
#include<map>
#include<list>

using namespace std;

extern pthread_mutex_t block_m;
extern uint64_t blockAllocatorSize;


class BlockAllocatorST
{
  public:
    BlockAllocatorST(uint64_t block_size_):block_size(block_size_), upperBound(0), currPosition(0) { }
    uint64_t allocate(uint64_t size);
    void clearBlocks();

  private:
    uint64_t block_size;
    list<uint64_t> allocated_blocks;
    uint64_t upperBound;
    uint64_t currPosition;
};


class BlockAllocatorMT
{
  public:
    BlockAllocatorMT();
    ~BlockAllocatorMT() { }
    void initiate();
    uint64_t allocate(uint64_t size);
    void clearBlocks(bool remove_thread_info = true);
    static uint64_t block_size;

  private:
    static uint64_t gCount;
    list<uint64_t> allocated_blocks;
    map< pthread_t, pair<uint64_t, uint64_t> > curr_blocks;
};


extern BlockAllocatorMT bamt;

template <class T> class MyCustomAlloc
{
  public:
    typedef T          value_type;
    typedef size_t     size_type;
    typedef ptrdiff_t  difference_type;
    typedef T*         pointer;
    typedef const T*   const_pointer;
    typedef T&         reference;
    typedef const T&   const_reference;

    MyCustomAlloc()      { }
    MyCustomAlloc(const MyCustomAlloc &obj) { }

  private:
    void operator =(const MyCustomAlloc &);

  public:
    template <class _Other> MyCustomAlloc(const MyCustomAlloc<_Other> &other) { }
    ~MyCustomAlloc() { }

    template <class U> struct rebind
    {
      typedef MyCustomAlloc<U> other;
    };

    pointer       address( reference r )       const { return &r; }
    const_pointer address( const_reference r ) const { return &r; }

    pointer allocate( size_type n, const void * /*hint*/=0 )
    {
      return (pointer) bamt.allocate(n * sizeof(T));
    }

    void deallocate( pointer p, size_type /*n*/ ) { }
    void construct( pointer p, const T& val ) { ::new (p) T(val); }
    void destroy( pointer p ) { p->~T(); }
    size_type max_size() const { return ((uint64_t)-1) / sizeof(T); }
};


template <class T> 
bool operator==( const MyCustomAlloc<T>& left, const MyCustomAlloc<T>& right )
{
  return true;
}

template <class T>
bool operator!=( const MyCustomAlloc<T>& left, const MyCustomAlloc<T>& right)
{
  return false;
}


#endif
